package com.xgk.boot.module.core.service.price;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgk.boot.framework.common.constant.ErrorCodeConstants;
import com.xgk.boot.framework.common.exception.util.ServiceExceptionUtil;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.module.core.controller.admin.price.vo.LgExtrasDictReqVO;
import com.xgk.boot.module.core.controller.admin.price.vo.LgExtrasDictSaveVO;
import com.xgk.boot.module.core.controller.admin.price.vo.LgExtrasDictVO;
import com.xgk.boot.module.core.dal.entity.price.LgExtrasDictDO;
import com.xgk.boot.module.core.dal.iservice.LgExtrasDictService;
import com.xgk.boot.module.core.dal.mapper.price.LgExtrasDictMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 支付管理，
 * 包含 cm-客户、su-供应商、sp-服务商 的收支付信息
 */
@Slf4j
@Service
public class LgExtrasDictServiceImpl extends ServiceImpl<LgExtrasDictMapper, LgExtrasDictDO> implements LgExtrasDictService {

    @Autowired
    private LgExtrasDictServiceImpl self;

    /**
     * 新增特殊规则字典
     *
     * @param lgExtrasDictSaveVO 特殊规则字典VO
     * @return 是否成功
     */
    public boolean addLgExtrasDict(LgExtrasDictSaveVO lgExtrasDictSaveVO) {
        LgExtrasDictDO one = this.getOne(new LambdaQueryWrapperX<LgExtrasDictDO>().eq(LgExtrasDictDO::getLgExtrasCode, lgExtrasDictSaveVO.getLgExtrasCode()));
        if(one != null){
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.COMMON_INFO_REPEAT,"extras code");
        }
        LgExtrasDictDO LgExtrasDictDO = new LgExtrasDictDO();
        BeanUtils.copyProperties(lgExtrasDictSaveVO, LgExtrasDictDO);
        return this.save(LgExtrasDictDO);
    }

    /**
     * 根据ID删除特殊规则字典
     *
     * @param id 特殊规则字典ID
     * @return 是否成功
     */
    public boolean deleteLgExtrasDictById(Long id) {
        //TODO: 如果绑定了价格规则，则不能被删除
        return this.removeById(id);
    }

    /**
     * 根据ID查询特殊规则字典
     *
     * @param id 特殊规则字典ID
     * @return 特殊规则字典VO
     */
    public LgExtrasDictVO getLgExtrasDictById(Long id) {
        LgExtrasDictDO LgExtrasDictDO = this.getById(id);
        if (LgExtrasDictDO == null) {
            return null;
        }
        LgExtrasDictVO LgExtrasDictVO = new LgExtrasDictVO();
        BeanUtils.copyProperties(LgExtrasDictDO, LgExtrasDictVO);
        return LgExtrasDictVO;
    }

    public PageResult<LgExtrasDictDO> pageList(LgExtrasDictReqVO lgExtrasDictReqVO){
        LambdaQueryWrapperX<LgExtrasDictDO> lgExtrasDictWrapperX = new LambdaQueryWrapperX<>();
        if(lgExtrasDictReqVO !=null){
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getLgExtrasName,lgExtrasDictReqVO.getLgExtrasName());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getLgExtrasCode,lgExtrasDictReqVO.getLgExtrasCode());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getServiceProvider,lgExtrasDictReqVO.getServiceProvider());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getExtrasDescription,lgExtrasDictReqVO.getExtrasDescription());
        }
        Page<LgExtrasDictDO> page=new Page<>(lgExtrasDictReqVO.getCurrent(),lgExtrasDictReqVO.getPageSize());
        Page<LgExtrasDictDO> pageRes = this.page(page, lgExtrasDictWrapperX);
        return new PageResult<>(pageRes.getRecords(), pageRes.getTotal(),lgExtrasDictReqVO.getCurrent(),lgExtrasDictReqVO.getPageSize());
    }

    /**
     * 查询所有特殊规则字典
     *
     * @return 特殊规则字典VO列表
     */
    public List<LgExtrasDictDO> getListLgExtrasDicts(LgExtrasDictReqVO lgExtrasDictReqVO) {
        LambdaQueryWrapperX<LgExtrasDictDO> lgExtrasDictWrapperX = new LambdaQueryWrapperX<>();
        if(lgExtrasDictReqVO !=null){
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getLgExtrasName,lgExtrasDictReqVO.getLgExtrasName());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getLgExtrasCode,lgExtrasDictReqVO.getLgExtrasCode());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getServiceProvider,lgExtrasDictReqVO.getServiceProvider());
            lgExtrasDictWrapperX.likeIfPresent(LgExtrasDictDO::getExtrasDescription,lgExtrasDictReqVO.getExtrasDescription());
        }
        List<LgExtrasDictDO> lgExtrasDictDOList = this.list(lgExtrasDictWrapperX);

        return lgExtrasDictDOList;
    }

    /**
     * 更新特殊规则字典（不更新ruleCode）
     *
     * @param lgExtrasDictVO 特殊规则字典VO
     * @return 是否成功
     */
    public boolean updateLgExtrasDict(LgExtrasDictSaveVO lgExtrasDictVO) {
        LgExtrasDictDO byId = this.getById(lgExtrasDictVO.getId());
        if(byId == null){
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.COMMON_INFO_EXISTS,"extras");
        }
        if(!lgExtrasDictVO.getLgExtrasCode().equals(byId.getLgExtrasCode())){
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.COMMON_INFO_NO_CHANGE,"extras code");
        }
        BeanUtils.copyProperties(lgExtrasDictVO, byId);
        // 创建更新条件，确保ruleCode不被更新
        // 不更新ruleCode
        return this.saveOrUpdate(byId);
    }
}
